/**
 * ECGA STAT SCRIPT
 * 
 * @author sunfengming <sunfengming@ec3s.com>
 * @copyright 2011 Xingchangxinda Inc.
 * @version SVN: $Id$
 */
var SiteStat = {
    'statImg' : null,
    'StatData' : {
        'startTime' : 0,
        'referer' : '',
        'statHash' : '',
        'getUrl' : '',
        'oldHash' : '',
        'remainingHour' : 0,
        'pvid': 0
    },
    'Url' : {
        // public method for url encoding
        encode : function(string) {
            return encodeURIComponent(this._utf8_encode(string));
        },
        // public method for url decoding
        decode : function(string) {
            return this._utf8_decode(decodeURIComponent(string));
        },
        // private method for UTF-8 encoding
        _utf8_encode : function(string) {
            string = string.replace(/\r\n/g, "\n");
            var utftext = "";
            for ( var n = 0; n < string.length; n++) {
                var c = string.charCodeAt(n);
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                } else if ((c > 127) && (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6) | 192);
                    utftext += String.fromCharCode((c & 63) | 128);
                } else {
                    utftext += String.fromCharCode((c >> 12) | 224);
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.fromCharCode((c & 63) | 128);
                }
            }
            return utftext;
        },
        // private method for UTF-8 decoding
        _utf8_decode : function(utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while (i < utftext.length) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                } else if ((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i + 1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                } else {
                    c2 = utftext.charCodeAt(i + 1);
                    c3 = utftext.charCodeAt(i + 2);
                    string += String.fromCharCode(((c & 15) << 12)
                            | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    },
    /**
     * Cookie 处理函数集
     */
    'Cookie' : {
        /**
         * 获取Cookie
         * 
         * @param {string} objName Cookie Name
         * 
         * @returns {mixed} 返回Cookie值，不存在返回false
         */
        'getCookie' : function(objName) {
            var arrStr = document.cookie.split("; ");
            for ( var i = 0; i < arrStr.length; i++) {
                var temp = arrStr[i].split("=");
                if (temp[0] == objName) {
                    return unescape(temp[1]);
                }
            }
            return false;
        },
        /**
         * 添加Coolie
         * 
         * @param {string} objName Cookie Name
         * @param {string} objValue Cookie Value
         * @param {int} objHours Cookie expires
         * 
         * @returns {void}
         */
        'addCookie' : function(objName, objValue, objHours) {
            var str = objName + "=" + escape(objValue);
            if (objHours > 0) {
                var date = new Date();
                var ms = objHours * 3600 * 1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            document.cookie = str;
        },
        /**
         * 删除Cookie，其实就是让指定的Cookie过期
         * 
         * @param {string} objName Cookie Name
         * 
         * @returns {void}
         */
        'delCookie' : function(objName) {
            var date = new Date();
            date.setTime(date.getTime() - 10000);
            document.cookie = name + "=a; expires=" + date.toGMTString();
        }
    },
    /**
     * 是否拥有访问记录
     * 
     * @returns {string} 记录Hash
     */
    'isOldVisitor' : function() {
        var oldHash = SiteStat.Cookie.getCookie('stat_hash');
        if (false === oldHash) {
            SiteStat.Cookie.addCookie('stat_hash', SiteStat.StatData.statHash, SiteStat.StatData.remainingHour);
            return SiteStat.StatData.statHash;
        } else {
            return oldHash;
        }
    },
    /**
     * 开始统计
     * 
     * @returns {void}
     */
    'beginStat' : function() {
        SiteStat.StatData.referer = SiteStat.Url.encode(document.referrer);
        SiteStat.statImg = document.createElement('img');
        SiteStat.statImg.id = 'stat_img';
        document.body.appendChild(SiteStat.statImg);
        SiteStat.statImg.style.width = '0px';
        var uvSrc = SiteStat.StatData.getUrl + '&referer='
            + SiteStat.StatData.referer + '&hash='
            + SiteStat.StatData.oldHash + '&rand=' + Math.random() + '&ac=uv&pvid=' + SiteStat.StatData.pvid;
        document.write('<img src="' + uvSrc + '" style="width:0px;" />');
        window.onbeforeunload = function(e) {
            SiteStat.statImg.src = SiteStat.StatData.getUrl + '&stime='
                    + SiteStat.StatData.startTime + '&rand=' + Math.random() + '&ac=st&pvid=' + SiteStat.StatData.pvid;
        }
    }
}
